package io.renren.modules.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.web.dao.TestpaperDao;
import io.renren.modules.web.entity.TestpaperEntity;
import io.renren.modules.web.entity.TestpapersonEntity;
import io.renren.modules.web.entity.TestquestionEntity;
import io.renren.modules.web.service.TestpaperService;
import io.renren.modules.web.service.TestpapersonService;
import io.renren.modules.web.service.TestquestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("testpaperService")
public class TestpaperServiceImpl extends ServiceImpl<TestpaperDao, TestpaperEntity> implements TestpaperService {

    @Autowired
    TestpapersonService testpapersonService;
    @Autowired
    TestquestionService testquestionService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");

        IPage<TestpaperEntity> page = this.page(
                new Query<TestpaperEntity>().getPage(params),
                new QueryWrapper<TestpaperEntity>()
                        .like(StringUtils.hasLength(key), "name", key)
                        .or()
                        .like(StringUtils.hasLength(key), "creator", key)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveTestPaper(TestpaperEntity testpaper) {
        baseMapper.insert(testpaper);
        List<Long> testPaperSonIds = testpaper.getTestPaperSons().stream().map(TestpapersonEntity::getId).collect(Collectors.toList());
        List<TestquestionEntity> testquestionEntities = testquestionService.listByIds(testPaperSonIds);
        List<TestpapersonEntity> testpapersonEntityList = testquestionEntities.stream().map(testquestionEntity -> {
            TestpapersonEntity testpapersonEntity = new TestpapersonEntity();
            testpapersonEntity.setPaperId(testpaper.getId());
            testpapersonEntity.setQuestion(testquestionEntity.getQuestion());
            testpapersonEntity.setAnswer(testquestionEntity.getAnswer());
            testpapersonEntity.setType(testquestionEntity.getType());
            testpapersonEntity.setScore(testquestionEntity.getScore());
            return testpapersonEntity;
        }).collect(Collectors.toList());

        testpapersonService.saveBatch(testpapersonEntityList);
    }

    @Override
    @Transactional
    public void deleteTestPaper(Long[] ids) {
        removeByIds(Arrays.asList(ids));
        // 删除关联的试题
        Arrays.stream(ids).forEach(id -> {
            testpapersonService.remove(new QueryWrapper<TestpapersonEntity>().eq("paper_id",id));
        });
    }

}